
// <copyright file="DictionaryEnumerator.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The enter or exit read lock.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Collections.Generic
{
    using System.Collections;
    using System.Collections.Generic;

    #region Delegates

    /// <summary>
    /// The enter or exit read lock.
    /// </summary>
    /// <param name="isEnter">
    /// The is enter.
    /// </param>
    public delegate void EnterOrExitReadLock(bool isEnter);

    #endregion Delegates

    /// <summary>
    /// The dictionary enumerator.
    /// </summary>
    /// <typeparam name="TKey">
    /// The type of the key.
    /// </typeparam>
    /// <typeparam name="TValue">
    /// The type of the value.
    /// </typeparam>
    public sealed class DictionaryEnumerator<TKey, TValue> : IEnumerator<KeyValuePair<TKey, TValue>>
    {
        #region Fields

        /// <summary>
        /// The callback.
        /// </summary>
        private readonly EnterOrExitReadLock _callback;

        /// <summary>
        /// The dictionary.
        /// </summary>
        private readonly IDictionary<TKey, TValue> _dictionary;

        /// <summary>
        /// The enumerator.
        /// </summary>
        private IEnumerator<KeyValuePair<TKey, TValue>> _enumerator;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="DictionaryEnumerator{TKey,TValue}"/> class.
        /// </summary>
        /// <param name="dictionary">
        /// The dictionary.
        /// </param>
        /// <param name="callback">
        /// The callback.
        /// </param>
        public DictionaryEnumerator(IDictionary<TKey, TValue> dictionary, EnterOrExitReadLock callback)
        {
            this._dictionary = dictionary;
            this._callback = callback;
        }

        #endregion Constructors and Finalizers

        #region Public Properties

        /// <summary>
        /// Gets the current.
        /// </summary>
        public KeyValuePair<TKey, TValue> Current
        {
            get
            {
                return this._enumerator.Current;
            }
        }

        #endregion Public Properties

        #region Explicit Interface Properties

        /// <summary>
        /// Gets the current.
        /// </summary>
        object IEnumerator.Current
        {
            get
            {
                return this._enumerator.Current;
            }
        }

        #endregion Explicit Interface Properties

        #region Public Methods and Operators

        /// <summary>
        /// The dispose.
        /// </summary>
        public void Dispose()
        {
            this._callback(false);
            this._enumerator = null;
        }

        /// <summary>
        /// The move next.
        /// </summary>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool MoveNext()
        {
            if (this._enumerator == null)
            {
                this._callback(true);
            }

            this._enumerator = this._dictionary.GetEnumerator();
            return this._enumerator.MoveNext();
        }

        /// <summary>
        /// The reset.
        /// </summary>
        public void Reset()
        {
            this._enumerator = null;
        }

        #endregion Public Methods and Operators
    }
}